package com.example.demo.contorller;

import java.util.List;
import java.util.concurrent.ExecutionException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.pojo.User;
import com.example.demo.service.*;

@RestController
public class TestContorller {
	
	@Autowired
	private UserService userService;
	/**********************调用Server——A的服务*************************/
	@GetMapping("/get")
	public String get() {
		return "hello";
	}

	
	/**********************调用Server——B的服务*************************/
	@RequestMapping("/getUserList")
	public List<User> getUserList(){
		return userService.getUserList();
		
	}
	
	
	/**********************调用Server——DB的服务*************************/
	@GetMapping("/getUser/{username}")
	public User getUser(@PathVariable String username) {
		
		return userService.getUser(username);
	}
	
	@GetMapping("/getUserById/{id}")
	public User getUserById(@PathVariable int id) {
		
		return userService.getUserById(id);
	}
	
	@GetMapping("/getString")
	public String getString() {
		return userService.getString();
	}
	@PostMapping("/insertUser")
	public int insertUser(@RequestParam String username,@RequestParam String password) {
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		return userService.insertUser(user);
	}
	
	@PostMapping("/updateUser")
	public int updateUser(@RequestParam String username,@RequestParam String password,@RequestParam int id) {
		User user = new User();
		user.setId(id);
		user.setUsername(username);
		user.setPassword(password);
		return userService.updateUser(user);
	}
	/**********************调用本地的服务*************************/
	@PostMapping(value = "/getString")
	public String getPrintString() {
		return "post String";
	}
	
	@DeleteMapping(value = "/getString")
	public String deleteString(@RequestParam(name = "uid") int id ) {
		//请求地址：localhost:7000/server-a/getString?uid=2
		//@RequestParam 请求时必须带指定的参数。如果后面不写name参数时，参数必须是以键值对来传递的。键必须跟形参一致。
		//否则用name进行映射。
		return "删除id为"+id+"的用户";	
	}
	
	@RequestMapping(params = "admin=root")
	public String print1() {
		return "只用当传入的键是=admin，值是root的时候才可以访问这个方法";
	}
}
